﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using VideoCarousel.Entity;

namespace VideoCarousel
{
    /// <summary>
    /// SetWindow.xaml 的交互逻辑
    /// </summary>
    public partial class SetWindow : Window
    {
        public AppConfig AppConfig { set; get; }
        public SetWindow()
        {
            InitializeComponent();
            string[] arr= new string[] { "default", "vlc" };
            _com.ItemsSource = arr;
            var d = AudioWindow.GetAllName();
            _diverCom.ItemsSource = d;
            this.Loaded += SetWindow_Loaded;
        }

        private void SetWindow_Loaded(object sender, RoutedEventArgs e)
        {
            this.DataContext = AppConfig;
        }

        private void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            ComboBoxItem comboBoxItem = _leftComboBox.SelectedItem as ComboBoxItem;
            var selectLeft = Convert.ToDouble(comboBoxItem.Content);

            AppConfig.Left = selectLeft;
        }

        private void SelectLocalWidth_click(object sender, RoutedEventArgs e)
        {
            AppConfig.Width = SystemParameters.PrimaryScreenWidth;
        }

        private void SelectLocalHeight_click(object sender, RoutedEventArgs e)
        {
            AppConfig.Height = SystemParameters.PrimaryScreenHeight;
        }

        private void SelectFolder_Click(object sender, RoutedEventArgs e)
        {
            var folder = OpenFolder();
            if(folder!=null)
                AppConfig.VideoFolder = folder;

        }
        private string OpenFolder()
        {
            Microsoft.WindowsAPICodePack.Dialogs.CommonOpenFileDialog openFileDialog = new Microsoft.WindowsAPICodePack.Dialogs.CommonOpenFileDialog();
            openFileDialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
            openFileDialog.IsFolderPicker = true;
            openFileDialog.Multiselect = true;
            if (openFileDialog.ShowDialog() == Microsoft.WindowsAPICodePack.Dialogs.CommonFileDialogResult.Ok)
            {
                return openFileDialog.FileName;
            }
            return null;

        }

        private void Cancel_Click(object sender, RoutedEventArgs e)
        {
            DialogResult = false;
        }

        private void Save_Click(object sender, RoutedEventArgs e)
        {
            DialogResult = true;

        }
        public static void Set()
        {
            AppConfig appConfig = new AppConfig();
            if (VCLib.Util.ConfigHelp.ReadConfig("appType") == "audio")
            {
                appConfig.AudioConfig =  Visibility.Visible;
                appConfig.VideoConfig = Visibility.Collapsed;
            }
            else
            {
                appConfig.AudioConfig = Visibility.Collapsed;
                appConfig.VideoConfig = Visibility.Visible;
            }
            appConfig.Left = VCLib.Util.ConfigHelp.ReadConfig<double>("left");
            appConfig.Top = VCLib.Util.ConfigHelp.ReadConfig<double>("top");
            appConfig.Width = VCLib.Util.ConfigHelp.ReadConfig<double>("width");
            appConfig.Height = VCLib.Util.ConfigHelp.ReadConfig<double>("height");
            appConfig.VideoFolder = VCLib.Util.ConfigHelp.ReadConfig("videoFolder");
            appConfig.IsFullScreen = Convert.ToBoolean(VCLib.Util.ConfigHelp.ReadConfig("isFullScreen"));
            appConfig.decode = VCLib.Util.ConfigHelp.ReadConfig("decode");
            appConfig.audioDevice = VCLib.Util.ConfigHelp.ReadConfig("audioDevice");
            appConfig.autoPlay = VCLib.Util.ConfigHelp.ReadConfig<bool>("autoPlay");

            SetWindow setWindow = new SetWindow();
            setWindow.AppConfig = appConfig;
            if (setWindow.ShowDialog() == true)
            {
                VCLib.Util.ConfigHelp.SaveConfig("left", appConfig.Left + "");
                VCLib.Util.ConfigHelp.SaveConfig("top", appConfig.Top + "");
                VCLib.Util.ConfigHelp.SaveConfig("width", appConfig.Width + "");
                VCLib.Util.ConfigHelp.SaveConfig("height", appConfig.Height + "");
                VCLib.Util.ConfigHelp.SaveConfig("videoFolder", appConfig.VideoFolder + "");
                VCLib.Util.ConfigHelp.SaveConfig("isFullScreen", appConfig.IsFullScreen + "");
                VCLib.Util.ConfigHelp.SaveConfig("decode", appConfig.decode + "");
                VCLib.Util.ConfigHelp.SaveConfig("audioDevice", appConfig.audioDevice + "");
                VCLib.Util.ConfigHelp.SaveConfig("autoPlay", appConfig.autoPlay + "");
                MessageBox.Show("保存成功，重启软件生效！");
            }

        }
    }
}
